package TwoSeach;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: TwoSeach
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-04  15:30
 * @Description: TODO
 * @Version: 1.0
 */
public class X的平方50 {
    public double myPow(double x, int n) {
        if (n == 0 || x == 1) {
            return 1;
        }
        if (n < 0) {
            return 1 / myPowerHelp(x, -n);
        }
        return myPowerHelp(x, n);
    }

    private double myPowerHelp(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        if (n % 2 != 0) {
            double half = myPowerHelp(x, n / 2);
            return half * half * x;
        } else {
            double half = myPowerHelp(x, n / 2);
            return half * half;
        }
    }
}
